package org.infodavid.common.impl.persistence;

import javax.persistence.PersistenceException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.infodavid.common.IBuilder;
import org.infodavid.common.dto.IUserConnectionListItem;
import org.infodavid.common.model.IUserConnection;
import org.infodavid.common.model.ModelFactory;
import org.infodavid.common.persistence.UserConnectionDataService;

/**
 * The Class AbstractUserConnectionDataService.
 */
public abstract class AbstractUserConnectionDataService extends UserConnectionDataService {

	/** The Constant log. */
	private static final Log LOGGER = LogFactory.getLog(AbstractUserConnectionDataService.class);

	/** The builder. */
	private final IBuilder<IUserConnection> builder = ModelFactory.getInstance().getBuilder(
	    IUserConnection.class);

	/**
	 * The Constructor.
	 */
	protected AbstractUserConnectionDataService() {
		super();
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.persistence.IDataService#create()
	 */
	public IUserConnection create() throws InstantiationException {
		return builder.create();
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.persistence.IDataService#copyTo(org.infodavid.common.model.IDataObject,
	 * boolean, boolean)
	 */
	public IUserConnection copyTo(final IUserConnection from, final boolean deep, final boolean asNew)
	    throws PersistenceException, InstantiationException {
		final boolean isDebugEnabled = LOGGER.isDebugEnabled();

		if (isDebugEnabled) {
			LOGGER.debug("Copying entity from " + from + " using deep = " + deep + ", asNew = " + asNew);
		}

		final IUserConnection to;

		if (from == null) {
			to = null;
		}
		else {
			to = builder.create();

			copyTo(from, to, asNew);
			to.setComment(from.getComment());
			to.setApprovalStatus(from.getApprovalStatus());
			to.setRecipient(from.getRecipient());
			to.setInitiator(from.getInitiator());
		}

		if (isDebugEnabled) {
			LOGGER.debug("Copied entity = " + to);
		}

		return to;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.persistence.IDataService#copyListItemTo(org.infodavid.common.model.IDataObject
	 * , boolean, boolean)
	 */
	public IUserConnectionListItem copyListItemTo(final IUserConnectionListItem from,
	    final boolean deep, final boolean asNew) throws PersistenceException, InstantiationException {
		final boolean isDebugEnabled = LOGGER.isDebugEnabled();

		if (isDebugEnabled) {
			LOGGER.debug("Copying entity from " + from + " using deep = " + deep + ", asNew = " + asNew);
		}

		final IUserConnection to;

		if (from == null) {
			to = null;
		}
		else {
			to = builder.create();

			copyTo(from, to, asNew);
			to.setComment(from.getComment());
			to.setApprovalStatus(from.getApprovalStatus());
			to.setRecipient(from.getRecipient());
			to.setInitiator(from.getInitiator());
		}

		if (isDebugEnabled) {
			LOGGER.debug("Copied entity = " + to);
		}

		return to;
	}
}
